package com.mikewong.tool.tesseract.Utils.StorageData;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.util.Base64;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.googlecode.leptonica.android.Convert;

/**
 * 外部存储的工具类
 */
public class ExternalStorageUtils {
    private static final String TAG = "Case";
    private static final String fileName = "json";
    private static final String type = "tessdata";

    /*使用方式：
        ExternalStorageUtils.getIdCardList().getData()
    * */
    public static IdCardList getIdCardList() {
        IdCardList idCardList = new IdCardList();
        idCardList = JSON.parseObject(readExternalStoragePublic(), IdCardList.class);
        return idCardList;
    }

    /**
     * 判断外部存储的空间是否可用
     *
     * @return 空间是否可用
     */
    public static boolean isExternalStorageUse() {
        boolean bl = false;
        //Environment 描述当前设备是否可用的类
        //根据Environment中的静态方法getExternalStorageState获取外部存储状态与Environment中的可读写的常量状态
        //比较  如果一致 说明外部存储的状态是可用
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            bl = true;
        }
        return bl;
    }

    /*
          //Convert to byte[]
        byte[] bytes = string.getBytes();

        //Convert back to String
        String s = new String(bytes);
        是否存储成功
     */
    public static boolean writeExternalStoragePublic(String str) {
        String readStr = readExternalStoragePublic2();
        String douhao = "";
        if (!readStr.equals("")) {
            douhao = ",";
        }
        String zuhe = readStr + douhao + str;
        byte[] content = zuhe.getBytes();

        boolean bl = false;
        if (isExternalStorageUse()) {//判断sdcard是否能够使用
            if (hasExternalStoragePublicDri(type)) {//判断外部存储的pictures是否存在
                try {
                    File parentFile = Environment.getExternalStoragePublicDirectory(type);
                    File file = new File(parentFile, fileName);//构建外部存储的file对象
                    FileOutputStream outputStream = new FileOutputStream(file);
                    outputStream.write(content);
                    bl = true;
                    outputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i(TAG, "当前文件夹不存在!");
            }
        } else {
            Log.i(TAG, "外部存储设备不可使用");
        }
        return bl;
    }

    /**
     * 根据指定的文件夹、名称读取指定文件夹下的文件
     *
     * @return 读取的字节数组
     */
    public static String readExternalStoragePublic() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        if (isExternalStorageUse()) {//外部存储路径是否可用
            if (hasExternalStoragePublicDri(type)) {//读取的文件夹是否存在
                try {
                    File parentFile = Environment.getExternalStoragePublicDirectory(type);
                    File file = new File(parentFile, fileName);
                    FileInputStream inputStream = new FileInputStream(file);
                    int temp = 0;
                    byte[] buff = new byte[1024];
                    while ((temp = inputStream.read(buff)) != -1) {
                        outputStream.write(buff, 0, temp);
                        outputStream.flush();
                    }
                    //Log.i(TAG, outputStream.toString());
                    inputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i(TAG, "当前文件夹不存在!");
            }
        } else {
            Log.i(type, "外部存储设备不可用!");
        }
        String s = "{\"data\":[" + new String(outputStream.toByteArray()) + "]}";
        return s;
    }

    private static String readExternalStoragePublic2() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        if (isExternalStorageUse()) {//外部存储路径是否可用
            if (hasExternalStoragePublicDri(type)) {//读取的文件夹是否存在
                try {
                    File parentFile = Environment.getExternalStoragePublicDirectory(type);
                    File file = new File(parentFile, fileName);
                    FileInputStream inputStream = new FileInputStream(file);
                    int temp = 0;
                    byte[] buff = new byte[1024];
                    while ((temp = inputStream.read(buff)) != -1) {
                        outputStream.write(buff, 0, temp);
                        outputStream.flush();
                    }
                    //Log.i(TAG, outputStream.toString());
                    inputStream.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                Log.i(TAG, "当前文件夹不存在!");
            }
        } else {
            Log.i(type, "外部存储设备不可用!");
        }
        String s = new String(outputStream.toByteArray());
        return s;
    }

    /**
     * 根据文件夹的类型判断外部存储的公共文件夹是否存在
     *
     * @param type 文件夹的类型
     * @return 是否存在
     */
    public static boolean hasExternalStoragePublicDri(String type) {
        boolean bl = false;
        File file = Environment.getExternalStoragePublicDirectory(type);
        //判断外部存储的公共路径下的dowloads文件夹是否存在
        if (file != null && file.exists()) {
            bl = true;
        }
        return bl;
    }

    /**
     * 删除单个文件
     *
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static void deleteFileAllJPG() {
        deleteFile("temp_cropped.jpg");
        deleteFile("temp.jpg");
    }

    public static boolean deleteFile(String fileName) {
        File file = new File(Environment.getExternalStoragePublicDirectory("tessdata") + fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                System.out.println("删除单个文件" + fileName + "成功！");
                return true;
            } else {
                System.out.println("删除单个文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println("删除单个文件失败：" + fileName + "不存在！");
            return false;
        }
    }

}